import {computed, ref} from 'vue'
import {defineStore} from 'pinia'


export const useUserStore = defineStore('user', () => {
    const user = ref({})
    const isLogin = ref(false)
    const token = ref("")

    user.value = JSON.parse(localStorage.getItem('userInfo'))
    isLogin.value = Boolean(localStorage.getItem('isLogin'))
    token.value = localStorage.getItem('token')

    function userLogin(userInfo, _token) {
        localStorage.setItem('userInfo', JSON.stringify(userInfo));
        localStorage.setItem('token', _token);
        localStorage.setItem('isLogin', 1);
        user.value = userInfo;
        token.value = _token;
        isLogin.value = true;
    }

    function userLogout() {
        localStorage.clear();
        user.value = {};
        isLogin.value = false;
    }

    return {user, isLogin, token, userLogin, userLogout}
})
